/** 
 * 
 * @author geloin 
 * @date 2012-5-5 涓婂崍9:31:52 
 */
package com.wcs.spring.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wcs.spring.entity.User;
import com.wcs.spring.service.UserService;
import com.wcs.spring.util.PageUtil;

@Controller
@RequestMapping(value = "mgt/teach")
public class TeachController {
	@Resource(name = "userService")
	private UserService userService;

	@RequestMapping(value = "teachList")
	public ModelAndView userList(User user, PageUtil pageUtil, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		PageInfo<User> pager = null;
		try {
			PageHelper.startPage(pageUtil.getPageNum(), pageUtil.getPageSize());
			List<User> list = this.userService.queryUserList(user, pageUtil);
			pager = new PageInfo(list);
		}
		catch (Exception e) {
			System.out.println("error:" + e.getMessage());
		}
		map.put("pager", pager);
		map.put("pageUtil", pageUtil);
		return new ModelAndView("mgt/teach/teach_list", map);
	}

	@RequestMapping(value = "toTeachSave")
	public ModelAndView toUserSave(HttpServletResponse response) throws Exception {
		return new ModelAndView("mgt/teach/teach_add");
	}

	@RequestMapping(value = "toTeachUpdate")
	public ModelAndView toUserUpdate(Integer id, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		User user = userService.queryUserById(id);
		map.put("user", user);
		return new ModelAndView("mgt/teach/teach_add", map);
	}

	@RequestMapping(value = "toTeachShow")
	public @ResponseBody
	User toUserShow(Integer id, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		User user = userService.queryUserById(id);
		// map.put("user", user);
		// return new ModelAndView("mgt/user/user_show", map);
		return user;
	}

	@RequestMapping(value = "teachSave")
	public ModelAndView userSave(User user, HttpServletResponse response) throws Exception {
		userService.addUser(user);
		return new ModelAndView("redirect:/mgt/teach/teachList.do");
	}

	@RequestMapping(value = "teachUpdate")
	public ModelAndView userUpdate(User user, HttpServletResponse response) throws Exception {
		userService.updateUser(user);
		return new ModelAndView("redirect:/mgt/teach/teachList.do");
	}

	@RequestMapping(value = "teachDelete")
	public ModelAndView userDelete(User user, HttpServletResponse response) throws Exception {
		userService.delUser(user);
		return new ModelAndView("redirect:/mgt/teach/teachList.do");
	}

}
